using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace NorthwindDAO
{
	public class Territories : Entity
	{
		private String _TerritoryID;
		private String _TerritoryDescription;
		private Int32 _RegionID;
	
		///Default constructor
		public Territories ()
		{
		
		}

        /// <summary>
        /// Constructor for joins. 
		/// Available CHILD tables to join with:
		///    - Employeeterritories ( Employeeterritories_fk_employeeterritories_territories )
		/// Available PARENT tables to join with:
		///    - Region
        /// </summary>
        public Territories(bool Flat, params Entity[] entities)
            : this()
		{
			foreach(Entity entity in entities)
			{
				string[] entityParts = entity.GetType().ToString().Split('.');
				string entityName = entityParts[entityParts.Length - 1];
				PropertyInfo pf = GetType().GetProperty((entityName));
				if (pf != null)
				{
					pf.SetValue(this,new EntityCollection(entity), null);
				}
                entityName = entityName + "_"; ;
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
			}
			SetFlat(Flat);
		}
        public Territories(params Entity[] entities)
            : this(true, entities)
		{
		}
	
		[IsNotNullable]
		[IsPK]
		[MaxLength(20)]
		public String Territoryid
		{
			get
			{
				return _TerritoryID;
			}
			set
			{
				_TerritoryID = value;
			}
		}
		[IsNotNullable]
		[MaxLength(50)]
		public String Territorydescription
		{
			get
			{
				return _TerritoryDescription;
			}
			set
			{
				_TerritoryDescription = value;
			}
		}
		[IsNotNullable]
    		public Int32 Regionid
		{
			get
			{
				return _RegionID;
			}
			set
			{
				_RegionID = value;
			}
		}
	
        #region Employeeterritories 
        private EntityCollection _Employeeterritories;
        [NotInTable]
        [TypeContained(typeof(Employeeterritories),"Employeeterritories")]
        public EntityCollection Employeeterritories
        {
            get
            {
                return _Employeeterritories;
            }

            set
            {
                _Employeeterritories = value;
            }
        }
        private Employeeterritories _Employeeterritories_;
        [NotInTable]
        public Employeeterritories Employeeterritories_
        {
            get
            {
                return _Employeeterritories_;
            }

            set
            {
                _Employeeterritories_ = value;
            }
        }
        #endregion
        #region Region 
        private EntityCollection _Region;
        [NotInTable]
        [TypeContained(typeof(Region),"Region")]
        public EntityCollection Region
        {
            get
            {
                return _Region;
            }

            set
            {
                _Region = value;
            }
        }
        private Region _Region_;
        [NotInTable]
        public Region Region_
        {
            get
            {
                return _Region_;
            }

            set
            {
                _Region_ = value;
            }
        }
        #endregion
        public override DBJoin GetJoin(string tableName)
        {
			if (string.Compare(tableName, "Employeeterritories", true) == 0)
			{
				ArrayList left = new ArrayList();
				ArrayList right = new ArrayList();
				left.Add(tableName + ".Territoryid");
				right.Add(GetType().Name + ".Territoryid");
				return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
			}
	        if (string.Compare(tableName, "Region", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
				left.Add(tableName + ".Regionid");
				right.Add(GetType().Name + ".Regionid");
				return new DBJoin(left, right, JoinTypeEnumeration.INNER);
			}
			return null;
		}
	}
}


